package avicit.bdp.dcs.plugin.rdbms;

import avicit.bdp.dcs.plugin.BaseDataxParam;
import avicit.bdp.dcs.plugin.DataxConstants;
import avicit.bdp.dcs.plugin.DataxJsonInterface;
import avicit.bdp.dcs.plugin.rdbms.param.RdbmsDataxParam;
import com.google.common.collect.ImmutableList;
import org.apache.commons.lang3.StringUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * @金航数码科技有限责任公司
 * @作者：liyb
 * @邮箱：liyb@avic-digital.com
 * @创建时间： 2020-08-01 11:39
 * @类说明：
 * @修改记录：
 */
public abstract class RdbmsDataxJson implements DataxJsonInterface {

    /**
     * 构建Reader json
     * @return
     */
    @Override
    public Map<String, Object> buildReader(BaseDataxParam param) {
        RdbmsDataxParam dataxParam = (RdbmsDataxParam) param;

        // 构建
        Map<String, Object> readerObj = new HashMap<>(4);
        readerObj.put(DataxConstants.NAME, getReaderName());
        Map<String, Object> parameterObj = new HashMap<>(8);
        Map<String, Object> connectionObj = new HashMap<>(4);

        parameterObj.put(DataxConstants.RDBMS_USERNAME, dataxParam.getJdbcUsername());
        parameterObj.put(DataxConstants.RDBMS_PASSWORD, dataxParam.getEncryptJdbcPassword());

        // 判断是否是 querySql
        if (StringUtils.isNotEmpty(dataxParam.getQuerySql())) {
            connectionObj.put(DataxConstants.RDBMS_QUERY_SQL, ImmutableList.of(dataxParam.getQuerySql()));
        } else {
            connectionObj.put(DataxConstants.RDBMS_TABLE, dataxParam.getTableList());
            parameterObj.put(DataxConstants.RDBMS_COLUMN, dataxParam.getColumnList());

            // 判断是否有where
            if (StringUtils.isNotEmpty(dataxParam.getWhereParam())) {
                parameterObj.put(DataxConstants.RDBMS_WHERE, dataxParam.getWhereParam());
            }
        }

        parameterObj.put(DataxConstants.RDBMS_SPLIT_PK, dataxParam.getSplitPk());
        connectionObj.put(DataxConstants.RDBMS_JDBC_URL, ImmutableList.of(dataxParam.getJdbcUrl()));
        parameterObj.put(DataxConstants.RDBMS_CONNECTION, ImmutableList.of(connectionObj));
        parameterObj.put(DataxConstants.RDBMS_FETCH_SIZE, DataxConstants.DEFAULT_FETCH_SIZE);
        readerObj.put(DataxConstants.PARAMETER, parameterObj);
        return readerObj;
    }

    /**
     * 构建Writer json
     * @return
     */
    @Override
    public Map<String, Object> buildWriter(BaseDataxParam param) {
        RdbmsDataxParam dataxParam = (RdbmsDataxParam) param;

        Map<String, Object> writerObj = new HashMap<>(4);
        writerObj.put(DataxConstants.NAME, getWriterName());

        Map<String, Object> parameterObj = new HashMap<>(8);
        parameterObj.put(DataxConstants.RDBMS_USERNAME, dataxParam.getJdbcUsername());
        parameterObj.put(DataxConstants.RDBMS_PASSWORD, dataxParam.getEncryptJdbcPassword());
        parameterObj.put(DataxConstants.RDBMS_COLUMN, dataxParam.getColumnList());
        // preSql
        parameterObj.put(DataxConstants.RDBMS_PRE_SQL, ImmutableList.of(dataxParam.getPreSql()));

        Map<String, Object> connectionObj = new HashMap<>(4);
        connectionObj.put(DataxConstants.RDBMS_TABLE, dataxParam.getTableList());
        connectionObj.put(DataxConstants.RDBMS_JDBC_URL, dataxParam.getJdbcUrl());

        parameterObj.put(DataxConstants.RDBMS_CONNECTION, ImmutableList.of(connectionObj));
        parameterObj.put(DataxConstants.RDBMS_BATCH_SIZE, DataxConstants.DEFAULT_BATCH_SIZE);

        parameterObj.put(DataxConstants.POLICY, dataxParam.getPolicyList());

        writerObj.put(DataxConstants.PARAMETER, parameterObj);
        return writerObj;
    }

}
